package datastream.api.operator.group;

import datastream.api.operator.group.pojo.InputModel;
import datastream.api.operator.group.source.Source;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSink;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Max {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);

        DataStreamSource<InputModel> source = env.addSource(new Source());

        KeyedStream<InputModel, Integer> keyBy = source.keyBy(new KeySelector<InputModel, Integer>() {
            @Override
            public Integer getKey(InputModel value) throws Exception {
                return value.getProduceId();
            }
        });

        SingleOutputStreamOperator<InputModel> sum = keyBy.max("income");

        DataStreamSink<InputModel> sink = sum.print();

        env.execute();

//        2> InputModel(produceId=1, income=10.0, describe=id1：第1条)
//        2> InputModel(produceId=2, income=20.0, describe=id2：第1条)
//        2> InputModel(produceId=3, income=30.0, describe=id3：第1条)
//        2> InputModel(produceId=2, income=20.0, describe=id2：第1条)
//        2> InputModel(produceId=1, income=10.0, describe=id1：第1条)
//        2> InputModel(produceId=3, income=30.0, describe=id3：第1条)
    }
}
